﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LibraryManagement.Custom_Controls.Data_Grid_View;
using LibraryManagement.Inventory;
namespace LibraryManagement
{
    public partial class frmSelectMembers : Form
    {
        private LibManagementEntities context;
        public enum eAction { Issue = 0, Submit = 1, PinCodes };
        public eAction mAction;
        private int yearId;
        private List<MemberDetail> dataSource;
        private object branchId;
        public frmSelectMembers()
        {
            InitializeComponent();
            context = new LibManagementEntities();
        }

        public frmSelectMembers(eAction mAction)
        {
            InitializeComponent();
            context = new LibManagementEntities();
            this.mAction = mAction;
            yearId = -1;
        }

        private void frmSelectMembers_Load(object sender, EventArgs e)
        {
            dgvMembers.Columns.Add(new libDataGridViewColumnSelect());
            SetGrid();
            cmbBranches.SetDataSource(context.Branches.ToList(), "BranchShortName", "BranchId");
        }

        private void SetGrid()
        {
            try
            {
                if (cmbBranches.SelectedIndex != -1 && yearId != -1)
                {
                    dataSource = context.MemberDetails.Where(x => x.Branch == (int)cmbBranches.SelectedValue && x.Year == yearId).ToList();
                }
                else if (cmbBranches.SelectedIndex != -1 && yearId == -1)
                {
                    dataSource = context.MemberDetails.Where(x => x.Branch == (int)cmbBranches.SelectedValue).ToList();

                }
                else if (yearId != -1 && cmbBranches.SelectedIndex == -1)
                {
                    dataSource = context.MemberDetails.Where(x => x.Year == yearId).ToList();

                }
                else if (cmbBranches.SelectedIndex == -1 && yearId == -1)
                    dataSource = context.MemberDetails.ToList();
                dgvMembers.DataSource = dataSource;
            }
            catch (Exception ex)
            {
                if (ex.Message == "Specified cast is not valid.") { }
                else
                    throw;
            }

        }

        private void dgvMembers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.ColumnIndex == 0)
            {
                decimal? id = (decimal?)dgvMembers.CurrentRow.Cells["Id"].Value;
                if (mAction == eAction.Issue)
                {
                    InvBookIssue OInvBookIssue = new InvBookIssue(id, this);
                    OInvBookIssue.MdiParent = this.MdiParent;
                    this.Enabled = false;
                    OInvBookIssue.Show();
                }
                else if (mAction == eAction.Submit)
                {
                    InvBookSubmit OInvBookSubmit = new InvBookSubmit(id, this);
                    OInvBookSubmit.MdiParent = this.MdiParent;
                    this.Enabled = false;
                    OInvBookSubmit.Show();
                }
                else if (mAction == eAction.PinCodes)
                {

                }
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbBranches_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetGrid();
            //if (ex.Message == "Specified cast is not valid.") { }
        }

        private void rdb1_CheckedChanged(object sender, EventArgs e)
        {
            yearId = 1;
            SetGrid();
        }

        private void rdb2_CheckedChanged(object sender, EventArgs e)
        {
            yearId = 2;
            SetGrid();
        }

        private void rdb3_CheckedChanged(object sender, EventArgs e)
        {
            yearId = 3;
            SetGrid();
        }

        private void rdb4_CheckedChanged(object sender, EventArgs e)
        {
            yearId = 4;
            SetGrid();
        }

        private void libRadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            yearId = -1;
            SetGrid();
        }

        private void chbAllBranches_CheckedChanged(object sender, EventArgs e)
        {
            if (chbAllBranches.Checked)
            {
                branchId = cmbBranches.SelectedValue;
                cmbBranches.SelectedIndex = -1;
                cmbBranches.Enabled = false;
            }
            else
            {
                if (branchId != null)
                {
                    cmbBranches.SelectedValue = branchId;
                }
                else
                {
                    cmbBranches.SelectedIndex = -1;
                }
                cmbBranches.Enabled = true;
            }
        }
    }
}
